home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung CD 2 (Tewi)(1994).iso / doc / mir / newlines.c < prev    next >
Text File  |  1992-07-02  |  6KB  |  193 lines

  1. /*
  2.  *  Usage - newlines  file_in  file_out  bytes_per_line
  3.  *
  4.  * NEWLINES Inserts carriage return and line feed every "file_in"
  5.  *          bytes.  Used to deblock data received in line blocks.
  6.  *
  7.  *  input:  A file in which data is broken into units in which every
  8.  *          unit is precisely the same length, and there are no line
  9.  *          feeds or carriage returns.  In the past this was a common
  10.  *          way of storing text and fixed length records.
  11.  *
  12.  *  output: The same data, expanded by the addition of carriage returns
  13.  *          and line feeds.  In this form, the data can be further
  14.  *          analyzed and processed using line-oriented programs.
  15.  *
  16.  *  writeup: MIR TUTORIAL ONE, topic 7
  17.  *
  18.  *  Written:    Douglas Lowry   Jan 10 92
  19.  *              Copyright (C) 1992 Marpex Inc.
  20.  *
  21.  *    The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
  22.  *    usage and co-ordination of the MIR family of programs to analyze,
  23.  *    prepare and index databases (small through gigabyte size), and
  24.  *    how to build integrated retrieval software around the MIR search
  25.  *    engine.  The fifth of the five MIR tutorial series explains how
  26.  *    to extend indexing capability into leading edge search-related
  27.  *    technologies.  For more information, GO IBMPRO on CompuServe;
  28.  *    MIR files are in the DBMS library.  The same files are on the
  29.  *    Canada Remote Systems BBS.  A diskette copy of the Introduction
  30.  *    is available by mail ($10 US... check, Visa or Mastercard);
  31.  *    diskettes with Introduction, Tutorial ONE software and the
  32.  *    shareware Tutorial ONE text cost $29.  Shareware registration
  33.  *    for a tutorial is also $29.
  34.  *
  35.  *    E-mail...
  36.  *                Compuserve  71431,1337
  37.  *                Internet    doug.lowry%canrem.com
  38.  *                UUCP        canrem!doug.lowry
  39.  *                Others:     doug.lowry@canrem.uucp
  40.  *
  41.  *    FAX...                  416 963-5677
  42.  *
  43.  *    "Snail mail"...         Douglas Lowry, Ph.D.
  44.  *                            Marpex Inc.
  45.  *                            5334 Yonge Street, #1102
  46.  *                            North York, Ontario
  47.  *                            Canada  M2N 6M2
  48.  *
  49.  *    Related database consultation and preparation services are
  50.  *    available through:
  51.  *              Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
  52.  *              North York, Ontario  Canada   M2J 4Z7
  53.  *              Tel.  416 492-3838   FAX  416 492-3843
  54.  *
  55.  *  This program is free software; you may redistribute it and/or
  56.  *  modify it under the terms of the GNU General Public License as
  57.  *  published by the Free Software Foundation; either version 2 of
  58.  *  the License, or (at your option) any later version.
  59.  *
  60.  *  This program is distributed in the hope that it will be useful,
  61.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  62.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  63.  *  GNU General Public License for more details.
  64.  *
  65.  *  You should have received a copy of the GNU General Public License
  66.  *  (file 05LICENS) along with this program; if not, write to the
  67.  *  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  68.  *  USA.
  69.  */
  70.  
  71. #include <stdio.h>
  72. #include <ctype.h>
  73.  
  74. #define     MAX_BYTES   1024
  75.  
  76. #define     repeat      for(;;)
  77.  
  78. typedef     enum        _bool
  79.             { FALSE = 0, TRUE = 1 }  Bool;
  80. /*
  81.  *  declarations
  82.  */
  83.  
  84.     void    Usage_(), process();
  85.     char    *Cmdname_() {   return( "newlines" );    }
  86.  
  87. /*
  88.  *  MAIN
  89.  */
  90.  
  91. main( argc, argv )
  92.     int argc;
  93.     char    **argv;
  94. {
  95.     FILE    *fp_in, *fp_out ;
  96.     char    c10 ;
  97.     int     bytes_per_ln ;
  98.  
  99.     /*  Usage - newlines  file_in  file_out  bytes_per_line */
  100.  
  101.     c10 = argv[1][0] ;
  102.     if( argc != 4 || c10 == '-' || c10 == '?' || c10 == '/' )
  103.         Usage_();
  104.  
  105.     if(( fp_in = fopen( argv[1], "rb" )) == NULL )
  106.     {
  107.         fprintf( stderr, "Can't open input file %s\n", argv[1] ) ;
  108.         Usage_() ;
  109.     }
  110.  
  111.     if(( fp_out = fopen( argv[2], "wb" )) == NULL )
  112.     {
  113.         fprintf( stderr, "Can't open output file %s\n", argv[2] ) ;
  114.         fclose( fp_in ) ;
  115.         Usage_() ;
  116.     }
  117.  
  118.     bytes_per_ln = atoi( argv[3] );
  119.     if( bytes_per_ln < 1 || bytes_per_ln > MAX_BYTES )
  120.     {
  121.         fprintf( stderr, "\nAllowable bytes per line is 1 to %d.\n",
  122.             MAX_BYTES );
  123.         Usage_() ;
  124.     }
  125.  
  126.     process( fp_in, fp_out, bytes_per_ln ) ;
  127.  
  128.     fclose( fp_in ) ;
  129.     fclose( fp_out ) ;
  130.     exit( 0 );
  131. }
  132. /*
  133.  *  Usage
  134.  */
  135.     void
  136. Usage_()
  137. {
  138.     fprintf( stderr,
  139.     "\nusage:  %s  file_in  file_out  bytes_per_line\n\n\
  140.         Inserts carriage return and line feed every \"file_in\"\n\
  141.         bytes.  Used to deblock data received in line blocks.\n\n\
  142. input:  A file in which data is broken into units in which every\n",
  143.             Cmdname_() ) ;
  144.     fprintf( stderr,
  145. "        unit is precisely the same length, and there are no line\n\
  146.         feeds or carriage returns.  In the past this was a common\n\
  147.         way of storing text and fixed length records.\n\n" ) ;
  148.     fprintf( stderr,
  149. "output: The same data, expanded by the addition of carriage returns\n\
  150.         and line feeds.  In this form, the data can be further\n\
  151.         analyzed and processed using line-oriented programs.\n\n\
  152. writeup: MIR TUTORIAL ONE, topic 7\n\n" ) ;
  153.     exit( 1 ) ;
  154. }
  155. /*
  156.  * PROCESS
  157.  */
  158.     void
  159. process( fp_in, fp_out, bytes_per )
  160.     FILE    *fp_in, *fp_out ;
  161.     int     bytes_per ;
  162. {
  163.     unsigned char   buf[ MAX_BYTES ] ;
  164.     int             line_no,
  165.                     over_120,       /*  To warn re line length  */
  166.                     ch,             /*  one character           */
  167.                     len, i ;
  168.  
  169.     repeat
  170.     {
  171.         if( !fread( buf, sizeof( char ), bytes_per, fp_in ))
  172.         {
  173.             if( feof( fp_in ))
  174.                 break ;
  175.             else
  176.             {
  177.                 fprintf( stderr, "Unable to read input file... FATAL.\n" );
  178.                 exit( 1 ) ;
  179.             }
  180.         }
  181.         if( !fwrite( buf, sizeof( char ), bytes_per, fp_out ))
  182.         {
  183.             fprintf( stderr, "Unable to write... FATAL.\n" );
  184.             exit( 1 ) ;
  185.         }
  186.  
  187.         fputc( '\015', fp_out ) ;
  188.         fputc( '\n', fp_out ) ;
  189.     }
  190.  
  191.     return ;
  192. }
  193.